<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/pintuer.css">
    <link rel="stylesheet" href="../css/bootstrap.css">

    <script src="../js/jquery-3.4.1.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap-table1.15.3.js"></script>
    <script src="../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/bootstrap-min-4.3.1.js"></script>
    <script src="../js/pintuer.js"></script>
    <script src="../js/initMain.js"></script>
    <script type="text/javascript">
        function initTable(){
            $("#tableInfo").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/sysRoles/queryAllRoles.action",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:true,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:5,
                //分页列表
                pageList: [5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,
                    }
                },
                columns:[

                    {title:"角色ID",field:"id",align:"center"},
                    {title:"角色名称",field:"name",align:"center"},
                    {title:"角色编号",field:"code",align:"center"},
                    {title:"角色说明",field:"descn",align:"center"},
                    {title:"操作",field:"id",align:"center",formatter:function (value,row,index) {
                            return "<a onclick='grantAndRevokeMenuFromRole("+JSON.stringify(row)+")' href='#'>授予/收回角色菜单</a>";
                        }},
                    {title:"操作",field:"id",align:"center",formatter:function (value,row,index) {
                            return "<a onclick='grantAndRevokeAuthorizationFromRole("+JSON.stringify(row)+")' href='#'>授予/收回角色权限</a>";
                        }},

                ]

            })
        }
        $(function () {
            initTable();
            //保存授予/收回角色菜单
            $("#saveGrantAndRevokeMenuFromRole").click(function () {
                var menuIds=[];
                var menuIdsNoCheck=[];
                for(var i=0;i<$("#queryMenuTable tbody tr").length;i++){
                    var menuId=$("#queryMenuTable tbody tr").eq(i).children().eq(0).children().eq(0).attr("id");
                    // alert($("#queryMenuTable tbody tr").eq(i).children().eq(0).children().eq(0).is(":checked"));
                    if($("#queryMenuTable tbody tr").eq(i).children().eq(0).children().eq(0).is(":checked")){
                        menuIds.push(menuId);
                    }else{
                        menuIdsNoCheck.push(menuId);
                    }
                }
                $.ajax({
                    url:"/sysMenuRole/grantAndRevokeMenuFromRole.action",
                    type:"post",
                    data: "menuIdsStr="+JSON.stringify(menuIds)+"&roleId="+roleId+"&menuIdsNoCheck="+JSON.stringify(menuIdsNoCheck),
                    success:function (data) {
                        alert("保存成功！");
                    }
                })
            })
            //保存授予/收回角色权限
            $("#saveGrantAndRevokeAuthorizationFromRole").click(function () {
                var sysAuthorizationIds=[];
                var sysAuthorizationIdsNoCheck=[];
                for(var i=0;i<$("#queryAuthorizationTable tbody tr").length;i++){
                    var sysAuthorizationId=$("#queryAuthorizationTable tbody tr").eq(i).children().eq(0).children().eq(0).attr("id");
                    if($("#queryAuthorizationTable tbody tr").eq(i).children().eq(0).children().eq(0).is(":checked")){
                        sysAuthorizationIds.push(sysAuthorizationId);
                    }else{
                        sysAuthorizationIdsNoCheck.push(sysAuthorizationId);
                    }
                }
                $.ajax({
                    url:"/sysRoleAuthorization/grantAndRevokeAuthorizationFromRole.action",
                    type:"post",
                    data: "sysAuthorizationIdsStr="+JSON.stringify(sysAuthorizationIds)+"&roleId="+roleId1+"&sysAuthorizationIdsNoCheck="+JSON.stringify(sysAuthorizationIdsNoCheck),
                    success:function (data) {
                        alert("保存成功！");
                    }
                })
            })
            $(document).on("click","#bigCheck",function(){
                if($(this).is(":checked")){
                    $(".menuCheck").prop("checked",true);
                }else{
                    $(".menuCheck").prop("checked",false);
                }
            })
            $(document).on("click",".menuCheck",function () {
                var len=$(".menuCheck:checked").length;
                //alert(len);
                if(len==0){
                    $("#bigCheck").prop("checked",false);
                }
            })
            $(document).on("click","#bigCheck1",function(){
                if($(this).is(":checked")){
                    $(".auCheck").prop("checked",true);
                }else{
                    $(".auCheck").prop("checked",false);
                }
            })
            $(document).on("click",".auCheck",function () {
                var len=$(".auCheck:checked").length;
                //alert(len);
                if(len==0){
                    $("#bigCheck1").prop("checked",false);
                }
            })
        })
        var roleId;
        function grantAndRevokeMenuFromRole(Role) {
            roleId=Role.id;
            $("input:checkbox").prop("checked",false);
            $("#queryMenuModel").modal("show");
            $("#queryMenuTable").bootstrapTable("destroy");

            $("#queryMenuTable").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/sysMenus/queryAllMenus",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:true,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:5,
                //分页列表
                pageList: [5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,
                    }
                },
                columns:[

                    {title:"<input type='checkbox' id='bigCheck'>",field:"id",align:"center",formatter:function (value,row,index) {
                            return "<input type='checkbox' class='menuCheck' id='"+value+"'/>"
                        }},
                    {title:"菜单ID",field:"id",align:"center"},
                    {title:"菜单名称",field:"name",align:"center"},
                    {title:"父菜单",field:"parentId",align:"center"},
                    {title:"菜单URL",field:"linkUrl",align:"center"}
                ],onLoadSuccess:function(){
                    $.ajax({
                        url:"/sysMenus/queryAllMenus1.action",
                        type:"post",
                        success:function (data) {
                            var allTableData = $("#queryMenuTable tbody tr");
                            for(var i = 0; i < allTableData.length; i++) {
                                for(var j=0;j<data.length;j++){
                                    var menu=data[j];
                                    if(menu.id == $("#queryMenuTable tbody tr").eq(i).children().eq(3).text() ) {
                                        // alert(menu.id);
                                        // alert($("#tableInfo tbody tr").eq(j).children().eq(2).text() );
                                        $("#queryMenuTable tbody tr").eq(i).children().eq(3).text( menu.name );
                                    }

                                }
                                if( $("#queryMenuTable tbody tr").eq(i).children().eq(3).text()<0){
                                    $("#queryMenuTable tbody tr").eq(i).children().eq(3).text("-");
                                }
                            }
                            //默认选中
                            $.ajax({
                                url:"/sysMenus/queryMenusByRoleId.action",
                                type:"post",
                                data:"roleId="+Role.id,
                                success:function (data) {
                                    var len=$("#queryMenuTable tbody tr").length;
                                    for(var i=0;i<len;i++){
                                        for(var j=0;j<data.length;j++){
                                            var menu=data[j];
                                            if($("#queryMenuTable tbody tr").eq(i).children().eq(1).text()==menu.id){
                                                $("#queryMenuTable tbody tr").eq(i).children().eq(0).children().eq(0).attr("checked","true");
                                            }
                                        }
                                    }
                                }
                            })
                        }
                    })

                }

            })
        }
        var roleId1;
        function grantAndRevokeAuthorizationFromRole(Role) {
            roleId1=Role.id;
            $("input:checkbox").prop("checked",false);

            $("#queryAuthorizationModel").modal("show");
            $("#queryAuthorizationTable").bootstrapTable("destroy");
            $("#queryAuthorizationTable").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/sysAuthorization/queryAllSysAuthorizations.action",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:true,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:5,
                //分页列表
                pageList: [5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,
                    }
                },
                columns:[

                    {title:"<input type='checkbox' id='bigCheck1'>",field:"auId",align:"center",formatter:function (value,row,index) {
                            return "<input type='checkbox' class='auCheck' id='"+value+"'/>"
                        }},
                    {title:"权限ID",field:"auId",align:"center"},
                    {title:"权限名称",field:"auName",align:"center"},
                    {title:"状态",field:"status",align:"center",formatter:function (value,row,index) {
                        if(value==1||value=="1"){
                            return "正常";
                        }else {
                            return "异常";
                        }
                    }}
                ],onLoadSuccess:function(){

                            //默认选中
                            $.ajax({
                                url:"/sysAuthorization/queryAuthorizationsByRoleId.action",
                                type:"post",
                                data:"roleId="+roleId1,
                                success:function (data) {
                                    var len=$("#queryAuthorizationTable tbody tr").length;
                                    for(var i=0;i<len;i++){
                                        for(var j=0;j<data.length;j++){
                                            var authorization=data[j];
                                            if($("#queryAuthorizationTable tbody tr").eq(i).children().eq(1).text()==authorization.auId){
                                                $("#queryAuthorizationTable tbody tr").eq(i).children().eq(0).children().eq(0).attr("checked","true");
                                            }
                                        }
                                    }
                                }
                            })

                }

            })
        }



    </script>
</head>
<body>
    <table id="tableInfo"></table>
    <div class="modal  fade" id="queryMenuModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title" id="info" >
                                    授予收回角色菜单
                                </h5>
                            </div>
                            <div class="col-2"></div>
                            <div class="col-3" style="position: absolute;right: 30px">
                                <button class="btn btn-primary" id="saveGrantAndRevokeMenuFromRole">确定</button>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div class="modal-body">
                    <table id="queryMenuTable">

                    </table>
                </div>

            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <div class="modal  fade" id="queryAuthorizationModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title"  >
                                    授予收回角色权限
                                </h5>
                            </div>
                            <div class="col-2"></div>
                            <div class="col-3" style="position: absolute;right: 30px">
                                <button class="btn btn-primary" id="saveGrantAndRevokeAuthorizationFromRole">确定</button>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div class="modal-body">
                    <table id="queryAuthorizationTable">

                    </table>
                </div>

            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

</body>
</html>